﻿using System;
using NH.Elateral.PipeManagement.Core.Interfaces;

namespace NH.Elateral.PipeManagement.Core
{
	public  class CommandParser: ICommandParser
	{
		private const string OPEN = "open";
		private const string CLOSE = "close";

		#region ICommandParser Members

		public ParseCommand Parse(string stringCommand)
		{
			ParseCommand cmd = new ParseCommand();
			stringCommand = stringCommand.ToLower();

			if (stringCommand.Contains(OPEN))
			{
				cmd.PipeState = true;
				cmd.PipeID = int.Parse(
							stringCommand.Replace(OPEN, String.Empty)
						);
			}
			else if (stringCommand.Contains(CLOSE))
			{
				cmd.PipeState = false;
				cmd.PipeID = int.Parse(
						stringCommand.Replace(CLOSE, String.Empty)
					);
			}
			else
			{
				throw new InvalidOperationException("Incoming Command is not in expected format");
			}

			return cmd;
		}

		#endregion
	}
}
